package strategic_monitor

import (
	"context"
	"errors"
	"git.mycaigou.com/gfyx/micro-gfyx-user-service/infrastructure/common/constants"
	pb "git.mycaigou.com/gfyx/micro-gfyx-user-service/proto/micro_gfyx_user_service_proto/micro-gfyx-user-service"
	"git.mycaigou.com/gfyx/micro-gfyx-user-service/service/strategic_monitor"
	"git.myscrm.cn/golang/stark/v4"
)

type strategicMonitorServer struct {
	strategicMonitorService strategic_monitor.StrategicMonitorServiceIface
}

func NewStrategicMonitorServer(
	strategicMonitorService strategic_monitor.StrategicMonitorServiceIface,
) pb.StrategicMonitorServiceServer {
	return &strategicMonitorServer{
		strategicMonitorService: strategicMonitorService,
	}
}

func (s *strategicMonitorServer) GetStrategicBidList(ctx context.Context, request *pb.GetStrategicBidListRequest) (*pb.GetStrategicBidListResponse, error) {
	if request.BidType != 1 && request.BidType != 2 {
		return nil, errors.New("公告类型错误")
	}

	resp, err := s.strategicMonitorService.GetStrategicBidList(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "grpc GetStrategicBidList request:%+v, err:%s", request, err.Error())
		return nil, err
	}

	return resp, nil
}

func (s *strategicMonitorServer) GetStrategicBidDetail(ctx context.Context, request *pb.GetStrategicBidDetailRequest) (*pb.GetStrategicBidDetailResponse, error) {
	if request.BidType != 1 && request.BidType != 2 {
		return nil, errors.New("公告类型错误")
	}

	resp, err := s.strategicMonitorService.GetStrategicBidDetail(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "grpc GetStrategicBidDetail request:%+v, err:%s", request, err.Error())
		return nil, err
	}

	return resp, nil
}

func (s *strategicMonitorServer) AuditStrategicBid(ctx context.Context, request *pb.AuditStrategicBidRequest) (*pb.AuditStrategicBidResponse, error) {
	if request.BidType != 1 && request.BidType != 2 {
		return nil, errors.New("公告类型错误")
	}

	resp, err := s.strategicMonitorService.AuditStrategicBid(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "grpc AuditStrategicBid request:%+v, err:%s", request, err.Error())
		return nil, err
	}

	return resp, nil
}

func (s *strategicMonitorServer) GetBidCompanyList(ctx context.Context, request *pb.GetBidCompanyListRequest) (*pb.GetBidCompanyListResponse, error) {
	resp, err := s.strategicMonitorService.GetBidCompanyList(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "grpc GetBidCompanyList request:%+v, err:%s", request, err.Error())
		return nil, err
	}
	return resp, nil
}

func (s *strategicMonitorServer) AuditBidCompany(ctx context.Context, request *pb.AuditBidCompanyRequest) (*pb.AuditBidCompanyResponse, error) {
	if request.Id == 0 {
		return nil, errors.New("id不能为空")
	}
	if request.AuditStatus != constants.AuditStatusAudited && request.AuditStatus != constants.AuditStatusReject {
		return nil, errors.New("审核状态错误")
	}
	resp, err := s.strategicMonitorService.AuditBidCompany(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "grpc AuditBidCompany request:%+v, err:%s", request, err.Error())
		return nil, err
	}
	return resp, nil
}

func (s *strategicMonitorServer) AddBidCompany(ctx context.Context, request *pb.AddBidCompanyRequest) (*pb.AddBidCompanyResponse, error) {
	if request.CompanyName == "" {
		return nil, errors.New("公司名称不能为空")
	}
	resp, err := s.strategicMonitorService.AddBidCompany(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "grpc AddBidCompany request:%+v, err:%s", request, err.Error())
		return nil, err
	}
	return resp, nil
}

func (s *strategicMonitorServer) GetBidCompanyDetail(ctx context.Context, request *pb.GetBidCompanyDetailRequest) (*pb.GetBidCompanyDetailResponse, error) {
	if request.Id == 0 {
		return nil, errors.New("id不能为空")
	}
	resp, err := s.strategicMonitorService.GetBidCompanyDetail(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "grpc GetBidCompanyDetail request:%+v, err:%s", request, err.Error())
		return nil, err
	}
	return resp, nil
}

func (s *strategicMonitorServer) GetIndustryList(ctx context.Context, request *pb.GetIndustryListRequest) (*pb.GetIndustryListResponse, error) {
	resp, err := s.strategicMonitorService.GetIndustryList(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "grpc GetIndustryList request:%+v, err:%s", request, err.Error())
		return nil, err
	}
	return resp, nil
}

func (s *strategicMonitorServer) AuditIndustry(ctx context.Context, request *pb.AuditIndustryRequest) (*pb.AuditIndustryResponse, error) {
	resp, err := s.strategicMonitorService.AuditIndustry(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "grpc AuditIndustry request:%+v, err:%s", request, err.Error())
		return nil, err
	}
	return resp, nil
}

func (s *strategicMonitorServer) AddIndustry(ctx context.Context, request *pb.AddIndustryRequest) (*pb.AddIndustryResponse, error) {
	resp, err := s.strategicMonitorService.AddIndustry(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "grpc AddIndustry request:%+v, err:%s", request, err.Error())
		return nil, err
	}
	return resp, nil
}

func (s *strategicMonitorServer) GetCompanyBidList(ctx context.Context, request *pb.GetCompanyBidListRequest) (*pb.GetCompanyBidListResponse, error) {
	if request.Id == 0 {
		return nil, errors.New("id不能为空")
	}
	resp, err := s.strategicMonitorService.GetCompanyBidList(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "grpc GetCompanyBidList request:%+v, err:%s", request, err.Error())
		return nil, err
	}
	return resp, nil
}

func (s *strategicMonitorServer) GetCompanyCooperateList(ctx context.Context, request *pb.GetCompanyCooperateListRequest) (*pb.GetCompanyCooperateListResponse, error) {
	if request.Id == 0 {
		return nil, errors.New("id不能为空")
	}
	resp, err := s.strategicMonitorService.GetCompanyCooperateList(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "grpc GetCompanyCooperateList request:%+v, err:%s", request, err.Error())
		return nil, err
	}
	return resp, nil
}

func (s *strategicMonitorServer) ImportBidCompany(ctx context.Context, request *pb.ImportBidCompanyRequest) (*pb.ImportBidCompanyResponse, error) {
	if request.FileId == "" {
		return nil, errors.New("文件id不能为空")
	}
	resp, err := s.strategicMonitorService.ImportBidCompany(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "grpc ImportBidCompany request:%+v, err:%s", request, err.Error())
		return nil, err
	}
	return resp, nil
}

func (s *strategicMonitorServer) DeleteBidAudit(ctx context.Context, request *pb.DeleteBidAuditRequest) (*pb.DeleteBidAuditResponse, error) {
	if request.Id == 0 {
		return nil, errors.New("id不能为空")
	}
	resp, err := s.strategicMonitorService.DeleteBidAudit(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "grpc DeleteBidAudit request:%+v, err:%s", request, err.Error())
		return nil, err
	}
	return resp, nil
}

func (s *strategicMonitorServer) DeleteCooperateCompany(ctx context.Context, request *pb.DeleteCooperateCompanyRequest) (*pb.DeleteCooperateCompanyResponse, error) {
	if request.Id == 0 {
		return nil, errors.New("id不能为空")
	}
	resp, err := s.strategicMonitorService.DeleteCooperateCompany(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "grpc DeleteCooperateCompany request:%+v, err:%s", request, err.Error())
		return nil, err
	}
	return resp, nil
}

func (s *strategicMonitorServer) GetIndustryByNames(ctx context.Context, request *pb.GetIndustryByNamesRequest) (*pb.GetIndustryByNamesResponse, error) {
	if len(request.IndustryNameArr) == 0 {
		return nil, errors.New("品类名称不能为空")
	}
	resp, err := s.strategicMonitorService.GetIndustryByNames(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "grpc GetIndustryByNames request:%+v, err:%s", request, err.Error())
		return nil, err
	}
	return resp, nil
}

func (s *strategicMonitorServer) GetBidCompanyByCodes(ctx context.Context, request *pb.GetBidCompanyByCodesRequest) (*pb.GetBidCompanyByCodesResponse, error) {
	if len(request.OrganizingCodeArr) == 0 {
		return nil, errors.New("社会信用代码不能为空")
	}
	resp, err := s.strategicMonitorService.GetBidCompanyByCodes(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "grpc GetBidCompanyByCodes request:%+v, err:%s", request, err.Error())
		return nil, err
	}
	return resp, nil
}

func (s *strategicMonitorServer) GetMonitorResult(ctx context.Context, request *pb.GetMonitorResultRequest) (*pb.GetMonitorResultResponse, error) {
	resp, err := s.strategicMonitorService.GetMonitorResult(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "grpc GetMonitorResult request:%+v, err:%s", request, err.Error())
		return nil, err
	}
	return resp, nil
}
